package com.yunc.modules.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.base.controller.CommonController;
import com.yunc.common.utils.R;
import com.yunc.modules.shop.entity.Address;
import com.yunc.modules.shop.service.IAddressService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 地址管理
 *
 * @author congcong tao
 * @date 2018/12/24 9:19
 */
@RestController
@RequestMapping("address")
public class AddressController extends CommonController {

    private static final Logger logger = LogManager.getLogger(AddressController.class);

    @Autowired
    private IAddressService addressService;
    
    @GetMapping("/page")
    public R page(Page<Address> page, Address address){
        addressService.page(page, new QueryWrapper<>(address));
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    @GetMapping("/list")
    public R list(Address address){
        QueryWrapper<Address> ew = new QueryWrapper<>();
        List<Address> list = addressService.list(ew);
        return R.ok().put("list", list).put("Address", address);
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable String id){
        Address address = addressService.getById(id);
        return R.ok().put("address", address);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody Address address){
        address.setCreateTime(new Date());
        boolean flag = addressService.save(address);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @PutMapping(value = "/update")
    public R update(@RequestBody Address address){
        boolean flag = addressService.updateById(address);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}
